package com.jokins.admin.config.sql;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

/**
 * 〈〉<br>
 *
 * @author Mr.Wang
 * @create 2024/11/12
 */
public class SshTunnelUtil {

    private static final int LOCAL_PORT = 3307;  // 本地转发端口，可以自定义
    private static final int REMOTE_PORT = 3306;  // 数据库服务端口
    private static final String DATABASE_HOST = "192.168.3.10";  // 云数据库内网IP
    private static final String SSH_USER = "root";  // 云服务器SSH用户名
    private static final String SSH_PASSWORD = "Wang991122..";  // 云服务器SSH密码
    private static final String SSH_HOST = "113.44.41.110";  // 云服务器公网IP
    private static Session session;

    public static void createTunnel() throws Exception {
        JSch jsch = new JSch();
        session = jsch.getSession(SSH_USER, SSH_HOST, 22);
        session.setPassword(SSH_PASSWORD);
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
        session.setPortForwardingL(LOCAL_PORT, DATABASE_HOST, REMOTE_PORT);
    }

    public static void closeTunnel() {
        if (session!= null) {
            session.disconnect();
        }
    }
}